{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "639ccbb6",
   "metadata": {},
   "source": [
    "--------------------\n",
    "* 2022/09/13 Week04\n",
    "* Python课堂笔记\n",
    "-------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28710f1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##  上周回顾； range()范围\n",
    "range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f2eac16a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# for+range 决定循环的次数\n",
    "for i in range(5):\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f2759eaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for i in range(1,10,3):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1be25248",
   "metadata": {},
   "outputs": [],
   "source": [
    "# start? Stop? step?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e41ad7e",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "* type():检查类型\n",
    "\n",
    "> 1. 数值\n",
    ">> 1. int 整数\n",
    ">> 2. float 浮点数\n",
    ">> 3.complex 复数/虚数\n",
    "\n",
    "> 2. 字符串\n",
    "\n",
    "> 3.对象\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a20fd790",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "## Number数值\n",
    "# int整数\n",
    "a = 9\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "42ecaffe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.5 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "# float 浮点数\n",
    "b = 10.5\n",
    "print(b,type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "857a2981",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5+3j) <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "## complex 虚数\n",
    "c = 5+3j\n",
    "print(c,type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41c445e1",
   "metadata": {},
   "source": [
    "## 2.字符串(string)\n",
    "\n",
    "> 1.关于字符串的基本内容\n",
    "\n",
    "> 2.关于字符串的方法及函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b77a9257",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alex <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name =\"Alex\" #双引号\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "076d4ca0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Joshua'me\""
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Joshua'me\" #双引号包单引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1c45ed05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Joshua\"me'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Joshua\"me' #单引号包双引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ca2c68f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Joshua'me\""
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Joshua\\'me'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "555c4b03",
   "metadata": {},
   "source": [
    "### 2.2 [关于字符串的方法及函数](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)\n",
    "\n",
    "> 一.字符串的定义\n",
    "\n",
    ">1.Single quotes: 'allows embedded \"double\" quotes'\n",
    "\n",
    ">2.Double quotes: \"allows embedded 'single' quotes\"\n",
    "\n",
    ">3.Triple quoted: '''Three single quotes''', \"\"\"Three double quotes\"\"\" \n",
    "\n",
    ">1.in检查成员对象\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a6f05ab",
   "metadata": {},
   "source": [
    "# 关于有序性\n",
    "title = \"广州南方学院坐落于广州市从化温泉镇\"\n",
    "print(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "867d91c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[0] #从左往右开始,取单一值，0为第一个字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0791a728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "815dfc65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(title) # 检查文本的总长度len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1d18691b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'镇'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[-1] #从右往左开始，-1为最后一个字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9232f4d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[0:6] #取一定范围的值，这种方法叫“切片”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f43e4065",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[:6]  #0可以省略不写，冒号需要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2afac0b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## enumerate 枚举，既可以获取到内容item，又可以获取到 索引值index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "e76924cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 广\n",
      "1 州\n",
      "2 南\n",
      "3 方\n",
      "4 学\n",
      "5 院\n",
      "6 坐\n",
      "7 落\n",
      "8 于\n",
      "9 广\n",
      "10 州\n",
      "11 市\n",
      "12 从\n",
      "13 化\n",
      "14 温\n",
      "15 泉\n",
      "16 镇\n"
     ]
    }
   ],
   "source": [
    "## 利用for循环 遍历文本信息\n",
    "for i,j in enumerate (title):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "8a444ff8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州在这段文本中\n",
      "广州在这段文本中\n"
     ]
    }
   ],
   "source": [
    "# 判断一个文本是否存在“广州”，例如文本  title  中的内容\n",
    "\n",
    "for i,j in enumerate (title):\n",
    "    if j ==\"广\":\n",
    "        if title[i+1] == \"州\":\n",
    "            print(\"广州在这段文本中\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1044e694",
   "metadata": {},
   "source": [
    "* 2.3 page59  in检查成员对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "ad38f0bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## page59  in检查成员对象 \n",
    "\"广州\" in title"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea9e1a81",
   "metadata": {},
   "source": [
    "* 字符串的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "ffcf4166",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,joshua\n"
     ]
    }
   ],
   "source": [
    "# 字符串不能和 数字进行连接\n",
    "print(\"hello\" +\",\"+\"joshua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "db2206f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 <class 'int'>\n",
      "1 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(1,type(1))\n",
    "print(\"1\",type(\"1\")) #字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "3afd814a",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for /: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [60]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m#字符串不可以用 来运算\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "#字符串不可以用 来运算\n",
    "\"3\"/\"2\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "5fc5896d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3/2 #数值可以运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "7e3b069b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 错误案例：没有分清字符串与数值的概念\n",
    "width = \"16“\n",
    "height = 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "767622f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "面积 = width * height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "3b33cb35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'161616161616161616'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "面积"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66afdad5",
   "metadata": {},
   "source": [
    "* 字符串的方法\n",
    "\n",
    ">1. str.capitalize():可以将首字母变成大写\n",
    ">2. str.count() 数量\n",
    ">3. str.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "e74aef2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"joshua\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "33329c72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Joshua'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "57a16304",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#count检查str中某个i出现的次数\n",
    "name.count(\"a\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "1abed33f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title.count(\"新媒体\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "54fe37fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['广州南方学院', '广州市从化温泉镇']"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title.split(\"坐落于\") #split拆分作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "28117e2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"文化经济.将积极起作用\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "6ff97456",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['文化经济', '将积极起作用']"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.split(\".\") #split也可以拆分标准符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7de5ded2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 可以搜索停止词字典，进行文本拆分，然后进一步分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd8d5b92",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fe3bdb56",
   "metadata": {},
   "source": [
    "# 自学内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79297a9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#not in检查对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7de809bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "title = [\"L\",\"O\",\"V\",\"E\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "defd20a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"U\" not in title:\n",
    "    title.append(\"U\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "86478bf5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['L', 'O', 'V', 'E', 'U']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e269148",
   "metadata": {},
   "outputs": [],
   "source": [
    "#从列表中删除对象. 对列表起作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0e029f51",
   "metadata": {},
   "outputs": [],
   "source": [
    "num = [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "745ca7d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "num.remove(3)  #remove直接指定列表中的对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f599bea1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f465a4b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#从列表中弹出对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fa929be3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.pop() #没有指定索引值，删掉最后一项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a6c55ebf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f5ede6d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.pop(1)#指定索引值,删除指定对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "26b90807",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84cf5c24",
   "metadata": {},
   "outputs": [],
   "source": [
    "#从对象扩展列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ee4ee2f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "num.extend([8,9]) #extend提供一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d3f23203",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 9]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "143aa33c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#从列表中插进一个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a5b378c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "num.insert(3,4) #3代表索引值，4代表插入的数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8efaf133",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 9, 4]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "be97b949",
   "metadata": {},
   "outputs": [],
   "source": [
    "num.insert(1,\"Joshua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b7f5b721",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'Joshua', 8, 9, 4]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37d75bff",
   "metadata": {},
   "outputs": [],
   "source": [
    "#字符串改成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f352ac54",
   "metadata": {},
   "outputs": [],
   "source": [
    "love = \"joshua\"\n",
    "plist = list(love) # 转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d398f350",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['j', 'o', 's', 'h', 'u', 'a']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0a7d8015",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "joshua\n"
     ]
    }
   ],
   "source": [
    "print(love)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff9883f0",
   "metadata": {},
   "outputs": [],
   "source": [
    " #列表转换成字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a4230e04",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = \"\".join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "49802627",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "joshua\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d063b75",
   "metadata": {},
   "source": [
    "#### 课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8b1c98b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"don't panic!\"\n",
    "\n",
    "plist = list(\"don't panic!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fa96d1bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(plist)\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8b33119d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(plist)  #注意“空格”也是字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0efcc958",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    plist.pop() #执行4次，删除对象\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])#pop最先执行，把后两个字符去掉，接着执行extend\n",
    "#这里的extend相当于append，添加了pop删除掉的后两个字符\n",
    "\n",
    "plist.insert(2,\"plist.pop(3)\") #这里用pop把空格删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "88c19129",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = \"\".join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "18906f75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', 'plist.pop(3)', 't', ' ', 'a', 'p']\n",
      "don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(plist)\n",
    "\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05fe4ae5",
   "metadata": {},
   "source": [
    "## 总结\n",
    "\n",
    "* 1.列表用于储存相关对象\n",
    "* 2.对象列表用\"中括号\"包围，列表对象之间用“逗号”隔开\n",
    "* 3.空列表为[]\n",
    "* 4.in用于检查对象是否存在列表\n",
    "* 5.insert，extend，append用于扩展列表\n",
    "* 6.pop，remove收缩列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd860c9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "63fcf6a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "first = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "cddfd9aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "second = first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "65de7841",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "101fe16f",
   "metadata": {},
   "outputs": [],
   "source": [
    "second.append(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "dfe5fc7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second   #first和second都是指向同一个数据，修改列表都会变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec49e7d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bae806ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "third = second.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "47615d7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "dc81a044",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 5, 5]\n",
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "third.append(5)\n",
    "print(third)\n",
    "print(second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dd04e7b",
   "metadata": {},
   "source": [
    "### 总结\n",
    "\n",
    "* 1.赋值(=)：用于另一个变量引用一个现有列表\n",
    "* 2.Copy：建立现有列表的副本，初始化一个新列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f0fb3d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表中中括号记法 start，stop，step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "5dc2c57b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for letter in range(0,10,2):\n",
    "    print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b46ca57",
   "metadata": {},
   "source": [
    "> letter[start:stop:step]\n",
    "* Start(开始)：控制范围从哪里开始。默认值为0\n",
    "* Stop（结束）：控制范围从哪里结束。 默认值为列表最大值\n",
    "* Step（步长）：每一步大小。默认值为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c88a7f97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表切片的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ae43c15b",
   "metadata": {},
   "outputs": [],
   "source": [
    "letter = ['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "6454aad3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', \"'\", 'p', 'i']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[0:10:3] #每3个字母选择一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "f2896a1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'o', 'n']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[:3]  #直到索引值的字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0383b222",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[3:] #跳过前三个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "dc242314",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'n', 't', 'p', 'n', 'c']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[::2] #每2个字母选择一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "3d2cf51b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[' ', 'T', 'h', 'e', ' ', 'g', 'o', 'o', 'd', ' ', 's', 't', 'a', 'r', ' ', 'm', 'e']\n"
     ]
    }
   ],
   "source": [
    "book =\" The good star me\"\n",
    "booklist = list(book)\n",
    "print(booklist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9332651a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T', 'h', 'e']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e7bc5d6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\".join(booklist[1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "5aa97624",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'me'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\".join(booklist[-2:]) #选择最后两个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bce2334",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
